package cn.iocoder.yudao.module.scrm.controller.admin.customizeproperty;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scrm.controller.admin.customizeproperty.vo.CustomizePropertyPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.customizeproperty.vo.CustomizePropertyRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.customizeproperty.vo.CustomizePropertySaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customizeproperty.CustomizePropertyDO;
import cn.iocoder.yudao.module.scrm.service.customizeproperty.CustomizePropertyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 自定义字段")
@RestController
@RequestMapping("/scrm/customize")
@Validated
public class CustomizePropertyController {

    @Resource
    private CustomizePropertyService customizePropertyService;

    @PostMapping("/create")
    @Operation(summary = "创建自定义字段")
    @PreAuthorize("@ss.hasPermission('scrm:customize-property:create')")
    public CommonResult<Long> createCustomizeProperty(@Valid @RequestBody CustomizePropertySaveReqVO createReqVO) {
        return success(customizePropertyService.createCustomizeProperty(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新自定义字段")
    @PreAuthorize("@ss.hasPermission('scrm:customize-property:update')")
    public CommonResult<Boolean> updateCustomizeProperty(@Valid @RequestBody CustomizePropertySaveReqVO updateReqVO) {
        customizePropertyService.updateCustomizeProperty(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除自定义字段")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:customize-property:delete')")
    public CommonResult<Boolean> deleteCustomizeProperty(@RequestParam("id") Long id) {
        customizePropertyService.deleteCustomizeProperty(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得自定义字段")
    @Parameter(name = "type", description = "类型", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:customize-property:query')")
    public CommonResult<List<CustomizePropertyRespVO>> getCustomizeProperty(@RequestParam("type") int type) {
        List<CustomizePropertyDO> customizeProperty = customizePropertyService.getCustomizeProperty(type);
        return success(BeanUtils.toBean(customizeProperty, CustomizePropertyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得自定义字段分页")
    @PreAuthorize("@ss.hasPermission('scrm:customize-property:query')")
    public CommonResult<PageResult<CustomizePropertyRespVO>> getCustomizePropertyPage(@Valid CustomizePropertyPageReqVO pageReqVO) {
        PageResult<CustomizePropertyDO> pageResult = customizePropertyService.getCustomizePropertyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomizePropertyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出自定义字段 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:customize-property:export')")
    @OperateLog(type = EXPORT)
    public void exportCustomizePropertyExcel(@Valid CustomizePropertyPageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomizePropertyDO> list = customizePropertyService.getCustomizePropertyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "自定义字段.xls", "数据", CustomizePropertyRespVO.class,
                BeanUtils.toBean(list, CustomizePropertyRespVO.class));
    }

}